package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.ProdTag;
import com.powernode.model.Result;
import com.powernode.service.ProdTagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: Tong Ziyu
 * @Date: 2025/2/7 11:04
 */
@RestController
@Api(tags = "商品分组接口管理")
@RequestMapping("prod/prodTag")
@Slf4j
public class ProdTagController {

    @Autowired
    private ProdTagService prodTagService;

    /**
     * 分页查询所有分组
     * @param current
     * @param size
     * @return
     */
    @ApiOperation("分页查询分组")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:prodTag:page')")
    public Result<Page> pageProdTagList(
            @RequestParam(value = "current") Long current,
            @RequestParam(value = "size") Long size,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Integer status
            ){
        Page<ProdTag> page = new Page<>(current, size);

        page = prodTagService.page(page,
                new LambdaQueryWrapper<ProdTag>()
                        .like(title != null && title.length() > 0, ProdTag::getTitle, title)
                        .eq(status != null, ProdTag::getStatus, status)
                        .orderByAsc(ProdTag::getSeq));

        return Result.success(page);
    }


    /**
     * 根据id查找分组
     * @param prodTagId
     * @return
     */
    @ApiOperation("根据id查找分组")
    @GetMapping("info/{prodTagId}")
    @PreAuthorize("hasAuthority('prod:prodTag:info')")
    public Result<ProdTag> infoByProdTageId(@PathVariable(value = "prodTagId") Long prodTagId){

        return Result.success(prodTagService.getById(prodTagId));
    }

    /**
     * 根据ProdTagId修改分类
     * @return
     */
    @ApiOperation("根据ProdTagId修改分组")
    @PutMapping()
    @PreAuthorize("hasAuthority('prod:prodTag:update')")
    public Result<String> updateProdTagById(@RequestBody ProdTag prodTag){
        Boolean b = prodTagService.modifyProdTagById(prodTag);

        return Result.handle(b);
    }

    /**
     * 新增分类
     * @return
     */
    @ApiOperation("新增分组")
    @PostMapping()
    @PreAuthorize("hasAuthority('prod:prodTag:save')")
    public Result<String> saveProdTag(@RequestBody ProdTag prodTag){
        Boolean b = prodTagService.saveProdTag(prodTag);

        return Result.handle(b);
    }

    /**
     * 根据ProdTag删除分组
     * @param ProdTagId
     * @return
     */
    @ApiOperation("根据ProdTag删除分组")
    @DeleteMapping("{ProdTagId}")
    @PreAuthorize("hasAuthority('prod:prodTag:delete')")
    public Result<String> deleteProdTagById(@PathVariable Long ProdTagId){

        boolean remove = prodTagService.remove(new LambdaQueryWrapper<ProdTag>().eq(ProdTag::getId, ProdTagId));

        return Result.handle(remove);
    }


    /**
     * 查询分组
     * @return
     */
    @ApiOperation("xxx")
    @GetMapping("listTagList")
    @PreAuthorize("hasAuthority('prod:prodTag:page')")
    public Result<List<ProdTag>> loadProductTagList(){
        return Result.success(prodTagService.queryProdTagList());
    }
}
